package com.frame.common.core.security.config;

import com.frame.common.core.security.entity.SecurityUser;
import com.frame.common.core.security.utils.TokenUtil;
import com.frame.stant.utils.R;
import com.frame.stant.utils.ResponseUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;


public class TokeLogoutHandler implements LogoutHandler {

    private TokenUtil tokenUtil;


    public TokeLogoutHandler(TokenUtil tokenUtil) {
        this.tokenUtil = tokenUtil;
    }
    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        SecurityUser currentUser = tokenUtil.getCurrentUser(request);
        if(Objects.nonNull(currentUser)){
            tokenUtil.delLoginUser(currentUser.getUuid());
        }
        ResponseUtil.out(response, R.success("退出成功"));

    }
}